// ==================== 主服务类 ====================
package org.chen.flight.service;

import lombok.extern.slf4j.Slf4j;
import org.chen.common.flight.request.FlightSearchRequest;
import org.chen.common.flight.response.OneWayFlightSearchResponse;
import org.chen.common.flight.response.RoundTripFlightSearchResponse;
import org.chen.flight.service.flight.FlightDataService;
import org.chen.flight.service.flight.FlightSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;


/**
 * 重构后的主服务类
 * 作为门面模式，协调各个子服务
 */
@Service
@Slf4j
public class FlightService {

    @Autowired
    private FlightSearchService flightSearchService;

    @Autowired
    private FlightDataService flightDataService;

    /**
     * 搜索单程机票
     */
    public OneWayFlightSearchResponse searchOneWayFlights(FlightSearchRequest request) {
        if (!"oneway".equals(request.getTripType())) {
            return OneWayFlightSearchResponse.error("请求类型错误，应为单程");
        }
        return flightSearchService.searchOneWayFlights(request);
    }

    /**
     * 搜索往返机票
     */
    public RoundTripFlightSearchResponse searchRoundTripFlights(FlightSearchRequest request) {
        if (!"roundtrip".equals(request.getTripType())) {
            return RoundTripFlightSearchResponse.error("请求类型错误，应为往返");
        }
        return flightSearchService.searchRoundTripFlights(request);
    }

    /**
     * 导出单程搜索结果
     */
    public ByteArrayResource exportOneWaySearchResult(OneWayFlightSearchResponse response,
                                                      FlightSearchRequest request,
                                                      boolean exportLowestPriceOnly) {
        return flightDataService.exportOneWaySearchResult(response, request, exportLowestPriceOnly);
    }

    /**
     * 导出往返搜索结果
     */
    public ByteArrayResource exportRoundTripSearchResult(RoundTripFlightSearchResponse response,
                                                         FlightSearchRequest request,
                                                         boolean exportLowestPriceOnly) {
        return flightDataService.exportRoundTripSearchResult(response, request, exportLowestPriceOnly);
    }
}